package com.answer.scoring;

import cn.hutool.json.JSONUtil;
import com.answer.model.dto.question.QuestionContentDTO;
import com.answer.model.dto.question.QuestionOptionDTO;
import com.answer.model.entity.Question;
import com.answer.model.entity.ScoringResult;
import com.answer.model.entity.UserAnswer;
import com.answer.model.enums.AppTypeEnum;
import com.answer.model.enums.ScoreStrategyEnum;
import com.answer.model.vo.QuestionVo;
import com.answer.service.QuestionService;
import com.answer.service.ScoringResultService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测评类
 */
@ScoringStrategyConfig(appType = 1,scoreType = 0)
public class TestScoringStrategyImpl implements ScoringStrategy{

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, Question question) {
        // 获取题目
        QuestionVo questionVo = QuestionVo.objToVo(question);
        List<QuestionContentDTO> questionContentDTO = questionVo.getQuestionContentDTO();
        //获取评分结果
        Long appId = question.getAppId();

        List<ScoringResult> scoringResults = scoringResultService.list(new LambdaQueryWrapper<ScoringResult>().eq(ScoringResult::getAppId, appId));
        if (scoringResults == null){
            scoringResults = scoringResultService.list(new LambdaQueryWrapper<ScoringResult>()
                    .eq(ScoringResult::getAppId, appId));
        }
        //评分
        ScoringResult bestQuestionResult = getBestQuestionResult(choices, questionContentDTO, scoringResults);
        //评分
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(question.getAppId());
//        userAnswer.setQuestionId(question.getId());
        userAnswer.setAppType(AppTypeEnum.TEST.getCode());
        userAnswer.setScoringStrategy(ScoreStrategyEnum.CUSTOM.getCode());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setUserId(1L);

        userAnswer.setResultId(bestQuestionResult.getId());
        userAnswer.setResultName(bestQuestionResult.getResultName());
        userAnswer.setResultDesc(bestQuestionResult.getResultDesc());
        userAnswer.setResultPicture(bestQuestionResult.getResultPicture());
        return userAnswer;
    }

    private ScoringResult getBestQuestionResult(List<String> answerList, List<QuestionContentDTO> questionContentDTO, List<ScoringResult> scoringResults) {
        Map<String, Integer> optionCount = new HashMap<>();
        for (QuestionContentDTO question : questionContentDTO) {
            for (String answer : answerList) {
                for (QuestionOptionDTO option : question.getOptions()) {
                    if (option.getKey().equals(answer)) {
                        String result = option.getResult();
                        optionCount.put(result, optionCount.getOrDefault(result, 0) + 1);
                    }
                }
            }
        }

        int maxScore = 0;
        ScoringResult maxScoreResult = scoringResults.get(0);

        for (ScoringResult result : scoringResults) {
            int score = JSONUtil.toList(result.getResultProp(), String.class)
                    .stream()
                    .mapToInt(prop -> optionCount.getOrDefault(prop, 0))
                    .sum();

            if (score > maxScore) {
                maxScore = score;
                maxScoreResult = result;
            }
        }

        return maxScoreResult;
    }
}
